/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.CheckOut;
import model.entities.Calls;
import model.entities.Customer;
import model.entities.CallsRole;
import model.entities.Room;

/**
 *
 * @author Hieu.Nguyen
 */
public class CallsDAO {
    private static CallsDAO callsDAO;

    public static CallsDAO getInstance() {
        if (callsDAO == null) {
            callsDAO = new CallsDAO();
        }
        return callsDAO;
    }
    
     public List<Calls> getCalls(boolean isActived) {
        List<Calls> listCalls = new ArrayList<Calls>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCalls where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Calls r = new Calls();
                r.setCallsID(rs.getInt("CallsID"));
                
                Room aa = RoomDAO.getInstance().getRoomByID(rs.getInt("RoomID"));                
                r.setRoom(aa);
                
                CallsRole aa1 = CallsRoleDAO.getInstance().getCallsRoleByID(rs.getInt("CallsRoleID"));                
                r.setCalsRole(aa1);
                
                Customer aa2 = CustomerDAO.getInstance().getCustomerByID(rs.getInt("CustomerID"));                
                r.setCustomer(aa2);
                
                r.setCallsDateStart(rs.getDate("CallsDateStart"));
                
                r.setTimeStart(rs.getString("TimeStart"));
                
                
                
                r.setCallsDateEnd(rs.getDate("CallsDateEnd"));
                
                r.setTimeEnd(rs.getString("TimeEnd"));
                
                r.setCallsContent(rs.getString("CallsContent"));

                listCalls.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCalls;
    }
     public boolean createCalls(Calls cal) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblCalls values (?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setInt(1, cal.getRoom().getRoomID());
            pstmt.setInt(2, cal.getCalsRole().getCallsRoleID());
            pstmt.setInt(3, cal.getCustomer().getCustomerID());
            pstmt.setDate(4, cal.getCallsDateStart());
            pstmt.setString(5, cal.getTimeStart());
            pstmt.setDate(6, cal.getCallsDateEnd());
            pstmt.setString(7, cal.getTimeEnd());
            pstmt.setString(8 , cal.getCallsContent());
            pstmt.setBoolean(9, cal.isIsActived());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
     public Calls getCallsByID(int callID) {
        Calls cl = new Calls();
       
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCalls where CallsID = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, callID);            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                cl.setCallsID(rs.getInt("CallsID"));
                
                Room rm = RoomDAO.getInstance().getRoomByID(rs.getInt("RoomID"));
                cl.setRoom(rm);
                
                Customer cm = CustomerDAO.getInstance().getCustomerByID(rs.getInt("CustomerID"));
                cl.setCustomer(cm);
                
                CallsRole crl = CallsRoleDAO.getInstance().getCallsRoleByID(rs.getInt("CallsRoleID"));
                cl.setCalsRole(crl);
                
                cl.setCallsDateStart(rs.getDate("CallsDateStart"));
                
                cl.setTimeStart(rs.getString("TimeStart"));
                
                cl.setCallsDateEnd(rs.getDate("CallsDateEnd"));
                
                cl.setTimeEnd(rs.getString("TimeEnd"));
                
                cl.setCallsContent(rs.getString("CallsContent"));
;
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cl;
     }
     
     public boolean deleteCalls(int callsID) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblCalls set isActived = 'false' where CallsID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, callsID);

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
     public boolean updateCalls(Calls calls) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblCalls set RoomID = ?, CallsRoleID = ?, CustomerID = ?, CallsDateStart = ?, TimeStart = ?, CallsDateEnd = ?, TimeEnd = ?, CallsContent = ? where CallsID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, calls.getRoom().getRoomID());
            pstmt.setInt(2, calls.getCalsRole().getCallsRoleID());
            pstmt.setInt(3, calls.getCustomer().getCustomerID());
            pstmt.setDate(4, calls.getCallsDateStart() );
            pstmt.setString(5, calls.getTimeStart());
            pstmt.setDate(6, calls.getCallsDateEnd());
            pstmt.setString(7, calls.getTimeEnd());
            pstmt.setString(8, calls.getCallsContent());
            pstmt.setInt(9, calls.getCallsID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
    }
    
}
